<?php

namespace app\admin\controller;

use app\admin\controller\Base;
use \think\Request;
use think\facade\View;
use app\common\model\TaskList as TaskListModel;
use app\common\model\GoodsList;

class TaskList extends Base
{
    /**
     * 任务列表
     */
    public function index(Request $request)
    {
        $keyword = trim(input('get.keyword'));
        if (!empty($keyword)) {
            $whe[] = ['title', 'like', '%' . $keyword . '%'];
        }
        $type = input('get.type');
        if (!empty($type)) {
            $whe[] = ['type', '=', $type];
        }
        $whe[] = ['id', '>', 0];
        $field = "*";
        $order = "addtime desc";
        $data = TaskListModel::getList($whe, $field, $order, $this->page);
        View::assign('list', $data['list']);
        View::assign('count', $data['count']);
        View::assign('page', $data['page']);
        return View::fetch("TaskList/index");

    }

    /**
     * 添加商品
     */
    public function add(Request $request)
    {
        if (!$request->isPost()) {

            return View::fetch("TaskList/add");
        } else {
            $data = input('post.');
            if (!$data['title']) {
                return $this->renderError("标题不能为空");
            }
            if (intval($data['number']) <= 0) {
                return $this->renderError("任务次数不能小于等于0");
            }
            if (intval($data['z_number']) <= 0) {
                return $this->renderError("欧气值不能小于等于0");
            }
            $data['addtime'] = time();
            $data['updatetime'] = time();
            $dd = TaskListModel::insertGetId($data);

            if ($dd) {
                return $this->renderSuccess("添加成功");
            } else {
                return $this->renderError("网络繁忙，请稍后");
            }
        }
    }

    /**
     * 编辑商品
     */
    public function edit(Request $request)
    {
        if (!$request->isPost()) {
            $id = $request->param('id');
            $goods = TaskListModel::getInfo(['id' => $id], '*');
            View::assign('data', $goods);
            return View::fetch("TaskList/edit");
        } else {
            $data = input('post.');
            if (!$data['title']) {
                return $this->renderError("标题不能为空");
            }
            if (intval($data['number']) <= 0) {
                return $this->renderError("任务次数不能小于等于0");
            }
            if (intval($data['z_number']) <= 0) {
                return $this->renderError("欧气值不能小于等于0");
            }
            $data['updatetime'] = time();
            TaskListModel::startTrans();
            $id = $data['id'];
            unset($data['id']);
            $dd = TaskListModel::where(['id' => $id])->update($data);

            if ($dd) {
                TaskListModel::commit();
                return $this->renderSuccess("编辑成功");
            } else {
                TaskListModel::rollback();
                return $this->renderError("网络繁忙，请稍后");
            }

        }
    }

    //上、下架商品
    public function cao(Request $request)
    {
        $id = $request->post('id/d');
        $status = $request->post('type/d');
//		if(!in_array($status,array(1,2,3))){
//			return $this->renderError("数据错误");
//		}
        $data = TaskListModel::where(['id' => $id])->whereNull('deltime')->field('id')->find();
        if (!$data) {
            return $this->renderError("数据不存在");
        }
        $result = TaskListModel::where(['id' => $id])->update(["deltime" => time()]);
        if ($result) {
            return $this->renderSuccess("操作成功");
        } else {
            return $this->renderError("网络繁忙，请稍后");
        }
    }


}
